package cn.songhaiqing.walle.ble_controller;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.util.Log;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import cn.songhaiqing.walle.global.Global;

public class BleConnectController {
    public static final int STATE_CONNECTED = 2;
    public static final int STATE_CONNECTFAIL = 3;
    public static final int STATE_CONNECTING = 1;
    public static final int STATE_DISCONNECTED = 0;
    private String TAG = "BluetoothController";
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothGatt mBluetoothGatt;
    private BluetoothManager mBluetoothManager;
    private int mConnectionState = 0;
    private Context mContext;
    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        /* class cn.songhaiqing.walle.ble_controller.BleConnectController.AnonymousClass1 */

        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            String mac = gatt.getDevice().getAddress();
            if (newState == 2) {
                BleConnectController.this.mConnectionState = 2;
                if (gatt.getDevice().getBondState() == 12) {
                    BleConnectController.this.logi("Waiting 1600 ms for a possible Service Changed indication...");
                    BleConnectController.this.waitFor(1600);
                }
                if (BleConnectController.this.sdkCallBack != null) {
                    BleConnectController.this.sdkCallBack.onConnectionStateChange(0, mac);
                }
            } else if (BleConnectController.this.mConnectionState == 2) {
                BleConnectController.this.mConnectionState = 0;
                BleConnectController.this.close();
                if (BleConnectController.this.sdkCallBack != null) {
                    BleConnectController.this.sdkCallBack.onConnectionStateChange(2, mac);
                }
            } else {
                BleConnectController.this.mConnectionState = 3;
                BleConnectController.this.close();
                if (BleConnectController.this.sdkCallBack != null) {
                    BleConnectController.this.sdkCallBack.onConnectionStateChange(1, mac);
                }
            }
        }

        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (BleConnectController.this.sdkCallBack == null) {
                return;
            }
            if (status == 0) {
                BleConnectController.this.sdkCallBack.onBluetoothServiceDiscover(true);
            } else {
                BleConnectController.this.sdkCallBack.onBluetoothServiceDiscover(false);
            }
        }

        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            BleConnectController.this.logMessageSend(characteristic.getValue());
        }

        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            byte[] value = characteristic.getValue();
            String mac = gatt.getDevice().getAddress();
            BleConnectController.this.logMessageReceive(value);
            if (BleConnectController.this.sdkCallBack != null) {
                BleConnectController.this.sdkCallBack.onSelfDeviceResponse(value, mac);
            }
        }

        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        }

        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            if (BleConnectController.this.sdkCallBack != null) {
                BleConnectController.this.sdkCallBack.onDescriptorWrite(status);
            }
        }

        public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
        }
    };
    private final Object mLock = new Object();
    private BleConnectCallBack sdkCallBack;

    /* JADX DEBUG: Failed to insert an additional move for type inference into block B:11:0x000d */
    /* JADX DEBUG: Multi-variable search result rejected for r6v0, resolved type: byte[] */
    /* JADX DEBUG: Multi-variable search result rejected for r0v0, resolved type: byte */
    /* JADX DEBUG: Multi-variable search result rejected for r0v2, resolved type: int */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v1 */
    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void logMessageSend(byte[] value) {
        String[] i = new String[value.length];
        for (int j = 0; j < i.length; j++) {
            byte a = value[j];
            if (a < 0) {
                a += 256;
            }
            i[j] = Integer.toHexString(a == 1 ? 1 : 0);
        }
        logi("send data:" + Arrays.toString(i));
    }

    /* JADX DEBUG: Failed to insert an additional move for type inference into block B:11:0x000d */
    /* JADX DEBUG: Multi-variable search result rejected for r6v0, resolved type: byte[] */
    /* JADX DEBUG: Multi-variable search result rejected for r0v0, resolved type: byte */
    /* JADX DEBUG: Multi-variable search result rejected for r0v2, resolved type: int */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r0v1 */
    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void logMessageReceive(byte[] value) {
        String[] i = new String[value.length];
        for (int j = 0; j < i.length; j++) {
            byte a = value[j];
            if (a < 0) {
                a += 256;
            }
            i[j] = Integer.toHexString(a == 1 ? 1 : 0);
        }
        logi("receive data:" + Arrays.toString(i));
    }

    /* access modifiers changed from: protected */
    public void waitFor(int millis) {
        synchronized (this.mLock) {
            try {
                this.mLock.wait((long) millis);
            } catch (InterruptedException e) {
                loge("Sleeping interrupted", e);
            }
        }
    }

    private void loge(String message, Throwable e) {
        // MyLogHelper.loge(this.TAG, message, e);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void logi(String message) {
        //MyLogHelper.logi(this.TAG, message);
    }

    public BluetoothGatt getmBluetoothGatt() {
        return this.mBluetoothGatt;
    }

    public BleConnectController(Context context) {
        this.mContext = context;
        initialize();
    }

    public Context getmContext() {
        return this.mContext;
    }

    public boolean initialize() {
        if (this.mBluetoothManager == null) {
            this.mBluetoothManager = (BluetoothManager) this.mContext.getSystemService(Context.BLUETOOTH_SERVICE);
            if (this.mBluetoothManager == null) {
                Log.e(this.TAG, "Unable to initialize BluetoothManager.");
                return false;
            }
        }
        this.mBluetoothAdapter = this.mBluetoothManager.getAdapter();
        if (this.mBluetoothAdapter != null) {
            return true;
        }
        Log.e(this.TAG, "Unable to obtain a BluetoothAdapter.");
        return false;
    }

    public int getConnectionState() {
        return this.mConnectionState;
    }

    public boolean connect(String address) {
        if (address == null) {
            throw new IllegalArgumentException("Mac can not be null.");
        } else if (address.split(":").length != 6) {
            throw new IllegalArgumentException("Mac format error.");
        } else if (this.mBluetoothAdapter == null || address == null) {
            Log.w(this.TAG, "BluetoothAdapter not initialized or unspecified address.");
            return false;
        } else if (getConnectionState() != 2 || this.sdkCallBack == null) {
            BluetoothDevice device = this.mBluetoothAdapter.getRemoteDevice(address);
            if (device == null) {
                Log.w(this.TAG, "Device not found.  Unable to connect.");
                return false;
            }
            this.mBluetoothGatt = device.connectGatt(this.mContext, false, this.mGattCallback);
            Log.d(this.TAG, "Trying to create a new connection.");
            this.mConnectionState = 1;
            return true;
        } else {
            this.sdkCallBack.onConnectionStateChange(3, address);
            return true;
        }
    }

    public void disconnect() {
        if (this.mBluetoothAdapter == null || this.mBluetoothGatt == null) {
            Log.w(this.TAG, "BluetoothAdapter not initialized");
        } else {
            this.mBluetoothGatt.disconnect();
        }
    }

    public void close() {
        if (this.mBluetoothGatt != null) {
            this.mBluetoothGatt.close();
            this.mBluetoothGatt = null;
        }
    }

    public List<BluetoothGattService> getSupportedGattServices() {
        if (this.mBluetoothGatt == null) {
            return null;
        }
        return this.mBluetoothGatt.getServices();
    }

    public boolean getRssiVal() {
        if (this.mBluetoothGatt == null) {
            return false;
        }
        return this.mBluetoothGatt.readRemoteRssi();
    }

    private void writeCharacteristic(BluetoothGatt mBluetoothGatt2, UUID uuid_service, UUID uuid_character, byte[] value) {
        BluetoothGattCharacteristic mBluetoothGattCharacteristic = getBluetoothGattCharacteristic(getBluetoothGattService(mBluetoothGatt2, uuid_service), uuid_character);
        if (mBluetoothGatt2 != null && mBluetoothGattCharacteristic != null) {
            mBluetoothGattCharacteristic.setValue(value);
            mBluetoothGattCharacteristic.setWriteType(1);
            mBluetoothGatt2.writeCharacteristic(mBluetoothGattCharacteristic);
        } else if (mBluetoothGatt2 == null) {
            Log.d(this.TAG, "mBluetoothGatt is null");
        } else if (mBluetoothGattCharacteristic == null) {
            Log.d(this.TAG, "mBluetoothGattCharacteristic is null");
        }
    }

    private void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
        if (this.mBluetoothAdapter == null || this.mBluetoothGatt == null) {
            Log.w(this.TAG, "BluetoothAdapter not initialized");
        } else {
            this.mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
        }
    }

    private void setCharactoristicNotifyAndWriteDescriptor(BluetoothGatt mBluetoothGatt2, UUID uuid_service, UUID uuid_characteristic, UUID uuid_descriptor) {
        BluetoothGattCharacteristic mBluetoothGattCharacteristic = getBluetoothGattCharacteristic(getBluetoothGattService(mBluetoothGatt2, uuid_service), uuid_characteristic);
        if (mBluetoothGatt2 != null && mBluetoothGattCharacteristic != null) {
            mBluetoothGatt2.setCharacteristicNotification(mBluetoothGattCharacteristic, true);
            BluetoothGattDescriptor bluetoothGattDescriptor = mBluetoothGattCharacteristic.getDescriptor(uuid_descriptor);
            if (bluetoothGattDescriptor != null) {
                bluetoothGattDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                mBluetoothGatt2.writeDescriptor(bluetoothGattDescriptor);
            }
        } else if (mBluetoothGatt2 == null) {
            Log.d(this.TAG, "mBluetoothGatt is null");
        } else if (mBluetoothGattCharacteristic == null) {
            Log.d(this.TAG, "mBluetoothGattCharacteristic is null");
        }
    }

    public void setCharactoristicNotify(UUID uuid_service, UUID uuid_characteristic) {
        BluetoothGattCharacteristic mBluetoothGattCharacteristic = getBluetoothGattCharacteristic(getBluetoothGattService(this.mBluetoothGatt, uuid_service), uuid_characteristic);
        if (this.mBluetoothGatt != null && mBluetoothGattCharacteristic != null) {
            this.mBluetoothGatt.setCharacteristicNotification(mBluetoothGattCharacteristic, true);
        } else if (this.mBluetoothGatt == null) {
            Log.d(this.TAG, "mBluetoothGatt is null");
        } else if (mBluetoothGattCharacteristic == null) {
            Log.d(this.TAG, "mBluetoothGattCharacteristic is null");
        }
    }

    private BluetoothGattService getBluetoothGattService(BluetoothGatt mBluetoothGatt2, UUID UUID_SERVICE) {
        if (mBluetoothGatt2 != null) {
            BluetoothGattService mBluetoothGattServer = mBluetoothGatt2.getService(UUID_SERVICE);
            if (mBluetoothGattServer != null) {
                return mBluetoothGattServer;
            }
            Log.d(this.TAG, "getBluetoothGattService, bluetoothgatt get service uuid:" + UUID_SERVICE + " is null");
        } else {
            Log.d(this.TAG, "mBluetoothGatt is null");
        }
        return null;
    }

    private BluetoothGattCharacteristic getBluetoothGattCharacteristic(BluetoothGattService mBluetoothGattService, UUID UUID_CHARACTERISTIC) {
        if (mBluetoothGattService != null) {
            BluetoothGattCharacteristic mBluetoothGattCharacteristic = mBluetoothGattService.getCharacteristic(UUID_CHARACTERISTIC);
            if (mBluetoothGattCharacteristic != null) {
                return mBluetoothGattCharacteristic;
            }
            Log.d(this.TAG, "getBluetoothGattCharacteristic, bluetoothGattServer get characteristic uuid:" + UUID_CHARACTERISTIC + " is null");
        } else {
            Log.d(this.TAG, "mBluetoothGattServer is null");
        }
        return null;
    }

    public void self_notifyAndWriteDescriptor(UUID uuid_service, UUID uuid_characteristic, UUID uuid_descriptor) throws NoConnectException {
        if (getConnectionState() == 2) {
            setCharactoristicNotifyAndWriteDescriptor(this.mBluetoothGatt, uuid_service, uuid_characteristic, uuid_descriptor);
            return;
        }
        throw new NoConnectException("no connect, please connect to your band first");
    }

    public boolean self_notify(UUID uuid_service, UUID uuid_characteristic) throws NoConnectException {
        if (getConnectionState() == 2) {
            BluetoothGattCharacteristic mBluetoothGattCharacteristic = getBluetoothGattCharacteristic(getBluetoothGattService(this.mBluetoothGatt, uuid_service), uuid_characteristic);
            if (this.mBluetoothGatt != null && mBluetoothGattCharacteristic != null) {
                return this.mBluetoothGatt.setCharacteristicNotification(mBluetoothGattCharacteristic, true);
            }
            if (this.mBluetoothGatt == null) {
                Log.d(this.TAG, "mBluetoothGatt is null");
            } else if (mBluetoothGattCharacteristic == null) {
                Log.d(this.TAG, "mBluetoothGattCharacteristic is null");
            }
            return false;
        }
        throw new NoConnectException("no connect, please connect to your band first");
    }

    public void self_writeData(UUID uuid_service, UUID uuid_character, byte[] value) throws NoConnectException {
        if (getConnectionState() == 2) {
            writeCharacteristic(this.mBluetoothGatt, uuid_service, uuid_character, value);
            return;
        }
        throw new NoConnectException("no connect, please connect to your band first");
    }

    public void findGattService() throws NoConnectException {
        if (getConnectionState() == 2) {
            this.mBluetoothGatt.discoverServices();
            return;
        }
        throw new NoConnectException("no connect, please connect to your band first");
    }

    public void set_notify_true() throws NoConnectException {
        if (getConnectionState() == 2) {
            setCharactoristicNotifyAndWriteDescriptor(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_NOTI, Global.UUID_DESCRIPTOR_CONFIGURATION);
            return;
        }
        throw new NoConnectException("no connect, please connect to your band first");
    }

    public void writeCharacteristic(byte[] value) throws NoConnectException {
        if (getConnectionState() == 2) {
            writeCharacteristic(this.mBluetoothGatt, Global.UUID_SERVICE, Global.UUID_CHARACTERISTIC_WRITE_AND_READ, value);
            return;
        }
        throw new NoConnectException("no connect, please connect to your band first");
    }

    public void registCallback(BleConnectCallBack callBack) {
        this.sdkCallBack = callBack;
    }

    public void unRegistCallback() {
        this.sdkCallBack = null;
    }
}
